package com.scsj.supermarket.view.activity.shopermodel;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.scsj.supermarket.R;
import com.scsj.supermarket.base.ShopCheckInBase;
import com.scsj.supermarket.bean.ShopCheckInCommitBean;
import com.scsj.supermarket.bean.ShoperCommitInfoBean;
import com.scsj.supermarket.bean.ShoperRequestInfoBean;
import com.scsj.supermarket.presenter.ShopCheckInPresenter;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;

import es.dmoral.toasty.Toasty;
import okhttp3.RequestBody;

/**
 * 商家申请入驻第四步
 */
public class RegisterFourStepActivity extends BaseActivity implements View.OnClickListener, ShopCheckInBase.IView {
    private ShopCheckInPresenter shopCheckInPresenter;
    private RelativeLayout sunmitBtnRl, confirmBtn;
    private PopupWindow mPopupWindow;
    private ShoperCommitInfoBean shoperCommitInfoBean;

    private RadioGroup shopTypeRg;
    private RadioButton radioButton;

    private EditText BankCardNumEt;
    private EditText OwnerNameEt;
    private EditText BankNameEt;
    private EditText BankSubNameEt;

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_register_four_step);
    }

    @Override
    protected void initView() {
        shopCheckInPresenter= new ShopCheckInPresenter(this);
        sunmitBtnRl = findViewById(R.id.registerfour_next_rl);
        shopTypeRg = findViewById(R.id.radio_shop_type);
        BankCardNumEt = findViewById(R.id.card_num);
        OwnerNameEt = findViewById(R.id.owner_name);
        BankNameEt = findViewById(R.id.which_bank);
        BankSubNameEt = findViewById(R.id.which_sub_bank);
    }

    @Override
    protected void initListener() {
        sunmitBtnRl.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        shoperCommitInfoBean= (ShoperCommitInfoBean) getIntent().getSerializableExtra("delivery_shopdata3");
        //获取第一个页面传递过来的数据
        //店铺名字
        String shopName = shoperCommitInfoBean.getShopName();
        //先打印一下看看
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.registerfour_next_rl:
                //弹框提示
                showSubmitWindow();
                break;
            case R.id.registerfour_confirm_rl:
                //店铺类型
                radioButton = findViewById(shopTypeRg.getCheckedRadioButtonId());
                //银行卡号
                String bankCardNum= BankCardNumEt.getText().toString();
                //开户人姓名
                String ownerName= BankCardNumEt.getText().toString();
                //开户行
                String bankName= BankCardNumEt.getText().toString();
                //开户支行
                String bankSubName= BankCardNumEt.getText().toString();

                if (TextUtils.isEmpty(bankCardNum)){
                    Toasty.info(RegisterFourStepActivity.this, "请输入银行卡号").show();
                    return;
                }else if ( TextUtils.isEmpty(ownerName)){
                    Toasty.info(RegisterFourStepActivity.this, "请输入开户人姓名").show();
                    return;
                }else if ( TextUtils.isEmpty(bankName)){
                    Toasty.info(RegisterFourStepActivity.this, "请输入开户行").show();
                    return;
                }else if ( TextUtils.isEmpty(bankSubName)){
                    Toasty.info(RegisterFourStepActivity.this, "请输入开户支行").show();
                    return;
                }else {
                    shoperCommitInfoBean.setBankCardNum(bankCardNum);
                    shoperCommitInfoBean.setOwnerName(ownerName);
                    shoperCommitInfoBean.setBankName(bankName);
                    shoperCommitInfoBean.setBankSubName(bankSubName);
                }
                break;
        }
    }

    private void showSubmitWindow() {

        View mlayoutPay = View.inflate(this, R.layout.registerfour_submit_pop, null);
        confirmBtn = mlayoutPay.findViewById(R.id.registerfour_confirm_rl);

        //设置背景半透明
        backgroundAlpha(0.5f);

        mPopupWindow = new PopupWindow(mlayoutPay, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setFocusable(true);
        mPopupWindow.showAtLocation(mlayoutPay, Gravity.CENTER, 0, 0);
        mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        mPopupWindow.setOnDismissListener(new RegisterFourStepActivity.popupDismissListener());

        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestToServe();
            }
        });
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();

        lp.alpha = bgAlpha; //0.0-1.0
        this.getWindow().setAttributes(lp);
    }

    @Override
    public void showSuccessMsg(String s, ShopCheckInCommitBean shopCheckInCommitBean) {

    }

    @Override
    public void showErrorMsg(String s) {

    }

    /**
     * 将背景透明度改回来
     */
    class popupDismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            backgroundAlpha(1f);
        }
    }

    private void requestToServe(){
        ///////////以下为请求接口设置参数/////////////////////////////////////
        ShoperRequestInfoBean shoperRequestInfoBean= new ShoperRequestInfoBean();

        /**
         * 配送信息配置
         */
        ShoperRequestInfoBean.DeliveryConfigBean deliveryConfigBean= new ShoperRequestInfoBean.DeliveryConfigBean();
        //标准距离
        deliveryConfigBean.setNormDist(Double.parseDouble(shoperCommitInfoBean.getStandardDistance()));
        //标准重量
        deliveryConfigBean.setNormWeight(Double.parseDouble(shoperCommitInfoBean.getStandardWeight()));
        //标准配送费
        deliveryConfigBean.setMinDeliveryFee(Integer.parseInt(shoperCommitInfoBean.getStandardPrice()));

        //增额配置
        deliveryConfigBean.setIncreaseDist(Double.parseDouble(shoperCommitInfoBean.getIncrementDistance()));
        deliveryConfigBean.setIncreaseDistFee(Integer.parseInt(shoperCommitInfoBean.getIncrementDistancePrice()));
        deliveryConfigBean.setIncreaseWeight(Double.parseDouble(shoperCommitInfoBean.getIncrementWeight()));
        deliveryConfigBean.setIncreaseWeightFee(Double.parseDouble(shoperCommitInfoBean.getIncrementWeightPrice()));

        //最高限制
        deliveryConfigBean.setMaxDist(Double.parseDouble(shoperCommitInfoBean.getFarthestDistance()));
        deliveryConfigBean.setMaxWeight(Double.parseDouble(shoperCommitInfoBean.getMostWeight()));

        /**
         * 店铺信息配置
         */
        ShoperRequestInfoBean.StoreBean storeBean= new ShoperRequestInfoBean.StoreBean();
        //店铺类型
        storeBean.setStoreType(Integer.parseInt(shoperCommitInfoBean.getShopType()));
        //店铺名称
        storeBean.setName(shoperCommitInfoBean.getShopName());
        //店铺地址
        storeBean.setAddressInfo(shoperCommitInfoBean.getShopAddress());

        /**
         * 地图相关信息配置
         */
        ShoperRequestInfoBean.StoreLocationBean storeLocationBean= new ShoperRequestInfoBean.StoreLocationBean();
        storeLocationBean.setAddress(shoperCommitInfoBean.getShopAddress());
        storeLocationBean.setLatitude(shoperCommitInfoBean.getLat());
        storeLocationBean.setLongitude(shoperCommitInfoBean.getLon());
        storeLocationBean.setCity(shoperCommitInfoBean.getCity());
        storeLocationBean.setBusiness(shoperCommitInfoBean.getBusiness());
        storeLocationBean.setProvince(shoperCommitInfoBean.getProvince());
        storeLocationBean.setDistrict(shoperCommitInfoBean.getDistrict());

        /**
         * 企业相关信息
         */
        ShoperRequestInfoBean.StoreRegisterDetailsBean storeRegisterDetailsBean= new ShoperRequestInfoBean.StoreRegisterDetailsBean();
        storeRegisterDetailsBean.setBusinessLicense(shoperCommitInfoBean.getLisencename());
        storeRegisterDetailsBean.setCompanyName(shoperCommitInfoBean.getLisencenameNum());
        storeRegisterDetailsBean.setLegalName(shoperCommitInfoBean.getBossName());
        storeRegisterDetailsBean.setLegalIds(shoperCommitInfoBean.getIdCardNum());
        storeRegisterDetailsBean.setOrganizationCode(shoperCommitInfoBean.getOriganNum());
        storeRegisterDetailsBean.setTelephone(shoperCommitInfoBean.getBossTel());

        /**
         * 银行相关信息
         */
        ShoperRequestInfoBean.UserBankCardForApplyBean userBankCardForApplyBean= new ShoperRequestInfoBean.UserBankCardForApplyBean();
        userBankCardForApplyBean.setBankCardNo(shoperCommitInfoBean.getBankCardNum());
        userBankCardForApplyBean.setUserName(shoperCommitInfoBean.getOwnerName());
        userBankCardForApplyBean.setAccountOpening(shoperCommitInfoBean.getBankName());
        userBankCardForApplyBean.setAccountOpeningBranch(shoperCommitInfoBean.getBankSubName());
        userBankCardForApplyBean.setBankCardNo("0");

        /**
         * 图片相关相关信息
         */
        ShoperRequestInfoBean.LogoResourceInfoBean logoResourceInfoBean= new ShoperRequestInfoBean.LogoResourceInfoBean();
        logoResourceInfoBean.setId(shoperCommitInfoBean.getShopLogoId());
        logoResourceInfoBean.setId(shoperCommitInfoBean.getshopInfoId());
        logoResourceInfoBean.setId(shoperCommitInfoBean.getLisenceImg());


        ///////////下面开始提交/////////////////////////////////////
        Gson gson = new Gson();
        String obj = gson.toJson(shoperRequestInfoBean);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=utf-8"), obj);
        shopCheckInPresenter.deliverData(this, body);
    }
}
